<?php


if (!defined('IN_DISCUZ')) {
    exit('Access Denied');
}

$pluginvars = array();
foreach (C::t('common_pluginvar')->fetch_all_by_pluginid($pluginid) as $var) {
    if (!strexists($var['type'], '_')) {
        C::t('common_pluginvar')->update_by_variable($pluginid, $var['variable'], array('type' => $var['type'] . '_thinker'));
    } else {
        $type = explode('_', $var['type']);
        if ($type[1] == 'thinker') {
            $var['type'] = $type[0];
        } else {
            continue;
        }
    }
    $pluginvars[$var['variable']] = $var;
}
require_once 'pluginvar.func.php';
if (!submitcheck('editsubmit')) {
    require_once 'setting.class.php';
    WxSetting::showResource();
    $operation = '';
    $wxurl = $_G['siteurl']."source/plugin/thinker_wx/wx.php";
    if ($pluginvars) {
        $_statInfo = array();
        $_statInfo['pluginName'] = $plugin['identifier'];
        $_statInfo['pluginVersion'] = $plugin['version'];
        $_statInfo['bbsVersion'] = DISCUZ_VERSION;
        $_statInfo['bbsRelease'] = DISCUZ_RELEASE;
        $_statInfo['timestamp'] = TIMESTAMP;
        $_statInfo['bbsUrl'] = $_G['siteurl'];
        $_statInfo['SiteID'] = '3FE1BDB6-D8C0-C362-56EB-EC1A7FE448E5';
        $_statInfo['bbsAdminEMail'] = $_G['setting']['adminemail'];
        $_statInfo['genuine'] = splugin_genuine($plugin['identifier']);
        showformheader("plugins&operation=config&do=$pluginid", 'enctype');
        showtableheader();
        echo '<div id="my_addonlist"></div>';
        showtitle($lang['plugins_config']);

        $extra = array();
        foreach ($pluginvars as $var) {
            if (strexists($var['type'], '_')) {
                continue;
            }
            $var['variable'] = $var['type']!='file'?'varsnew[' . $var['variable'] . ']': $var['variable'];
            if ($var['type'] == 'number') {
                $var['type'] = 'text';
            } elseif ($var['type'] == 'select') {
                $var['type'] = "<select name=\"$var[variable]\">\n";
                foreach (explode("\n", $var['extra']) as $key => $option) {
                    $option = trim($option);
                    if (strpos($option, '=') === FALSE) {
                        $key = $option;
                    } else {
                        $item = explode('=', $option);
                        $key = trim($item[0]);
                        $option = trim($item[1]);
                    }
                    $var['type'] .= "<option value=\"" . dhtmlspecialchars($key) . "\" " . ($var['value'] == $key ? 'selected' : '') . ">$option</option>\n";
                }
                $var['type'] .= "</select>\n";
                $var['variable'] = $var['value'] = '';
            } elseif ($var['type'] == 'selects') {
                $var['value'] = dunserialize($var['value']);
                $var['value'] = is_array($var['value']) ? $var['value'] : array($var['value']);
                $var['type'] = "<select name=\"$var[variable][]\" multiple=\"multiple\" size=\"10\">\n";
                foreach (explode("\n", $var['extra']) as $key => $option) {
                    $option = trim($option);
                    if (strpos($option, '=') === FALSE) {
                        $key = $option;
                    } else {
                        $item = explode('=', $option);
                        $key = trim($item[0]);
                        $option = trim($item[1]);
                    }
                    $var['type'] .= "<option value=\"" . dhtmlspecialchars($key) . "\" " . (in_array($key, $var['value']) ? 'selected' : '') . ">$option</option>\n";
                }
                $var['type'] .= "</select>\n";
                $var['variable'] = $var['value'] = '';
            } elseif ($var['type'] == 'date') {
                $var['type'] = 'calendar';
                $extra['date'] = '<script type="text/javascript" src="static/js/calendar.js"></script>';
            } elseif ($var['type'] == 'datetime') {
                $var['type'] = 'calendar';
                $var['extra'] = 1;
                $extra['date'] = '<script type="text/javascript" src="static/js/calendar.js"></script>';
            } elseif ($var['type'] == 'forum') {
                require_once libfile('function/forumlist');
                $var['type'] = '<select name="' . $var['variable'] . '"><option value="">' . cplang('plugins_empty') . '</option>' . forumselect(FALSE, 0, $var['value'], TRUE) . '</select>';
                $var['variable'] = $var['value'] = '';
            } elseif ($var['type'] == 'forums') {
                $var['description'] = ($var['description'] ? (isset($lang[$var['description']]) ? $lang[$var['description']] : $var['description']) . "\n" : '') . $lang['plugins_edit_vars_multiselect_comment'] . "\n" . $var['comment'];
                $var['value'] = dunserialize($var['value']);
                $var['value'] = is_array($var['value']) ? $var['value'] : array();
                require_once libfile('function/forumlist');
                $var['type'] = '<select name="' . $var['variable'] . '[]" size="10" multiple="multiple"><option value="">' . cplang('plugins_empty') . '</option>' . forumselect(FALSE, 0, 0, TRUE) . '</select>';
                foreach ($var['value'] as $v) {
                    $var['type'] = str_replace('<option value="' . $v . '">', '<option value="' . $v . '" selected>', $var['type']);
                }
                $var['variable'] = $var['value'] = '';
            } elseif (substr($var['type'], 0, 5) == 'group') {
                if ($var['type'] == 'groups') {
                    $var['description'] = ($var['description'] ? (isset($lang[$var['description']]) ? $lang[$var['description']] : $var['description']) . "\n" : '') . $lang['plugins_edit_vars_multiselect_comment'] . "\n" . $var['comment'];
                    $var['value'] = dunserialize($var['value']);
                    $var['type'] = '<select name="' . $var['variable'] . '[]" size="10" multiple="multiple"><option value=""' . (@in_array('', $var['value']) ? ' selected' : '') . '>' . cplang('plugins_empty') . '</option>';
                } else {
                    $var['type'] = '<select name="' . $var['variable'] . '"><option value="">' . cplang('plugins_empty') . '</option>';
                }
                $var['value'] = is_array($var['value']) ? $var['value'] : array($var['value']);

                $query = C::t('common_usergroup')->range_orderby_credit();
                $groupselect = array();
                foreach ($query as $group) {
                    $group['type'] = $group['type'] == 'special' && $group['radminid'] ? 'specialadmin' : $group['type'];
                    $groupselect[$group['type']] .= '<option value="' . $group['groupid'] . '"' . (@in_array($group['groupid'], $var['value']) ? ' selected' : '') . '>' . $group['grouptitle'] . '</option>';
                }
                $var['type'] .= '<optgroup label="' . $lang['usergroups_member'] . '">' . $groupselect['member'] . '</optgroup>' .
                    ($groupselect['special'] ? '<optgroup label="' . $lang['usergroups_special'] . '">' . $groupselect['special'] . '</optgroup>' : '') .
                    ($groupselect['specialadmin'] ? '<optgroup label="' . $lang['usergroups_specialadmin'] . '">' . $groupselect['specialadmin'] . '</optgroup>' : '') .
                    '<optgroup label="' . $lang['usergroups_system'] . '">' . $groupselect['system'] . '</optgroup></select>';
                $var['variable'] = $var['value'] = '';
            } elseif ($var['type'] == 'extcredit') {
                $var['type'] = '<select name="' . $var['variable'] . '"><option value="">' . cplang('plugins_empty') . '</option>';
                foreach ($_G['setting']['extcredits'] as $id => $credit) {
                    $var['type'] .= '<option value="' . $id . '"' . ($var['value'] == $id ? ' selected' : '') . '>' . $credit['title'] . '</option>';
                }
                $var['type'] .= '</select>';
                $var['variable'] = $var['value'] = '';
            }
            s_showsetting(isset($lang[$var['title']]) ? $lang[$var['title']] : dhtmlspecialchars($var['title']), $var['variable'], $var['value'], $var['type'], '', 0, isset($lang[$var['description']]) ? $lang[$var['description']] : nl2br(dhtmlspecialchars($var['description'])), dhtmlspecialchars($var['extra']), '', true);
        }
        showtablerow('', array(''), array(
           "微信URL : ".$wxurl
        ));
        showtablerow('', array(''), array(
            "<i>".lang('plugin/thinker_wx', 'subscribe')."</i>"
        ));
        showtablerow('', array('class="td29"'), array(
            "<textarea class=\"tarea\" name=\"subscribe\" id=\"res_subscribe\" rows=\"5\" cols=\"40\">".dhtmlspecialchars($_G['setting']['subscribe_reply'])."</textarea>"
            ."<br /><a href=\"javascript:;\" id=\"rsel\" onclick=\"showResource('res_subscribe')\">".lang('plugin/thinker_wx', 'source_select')."</a>"
        ));

        showsubmit('editsubmit');
        showtablefooter();
        showformfooter();
        echo implode('', $extra);

    }

} else {

    if (is_array($_GET['varsnew'])) {
        foreach ($_GET['varsnew'] as $variable => $value) {
            if (isset($pluginvars[$variable])) {
                if ($pluginvars[$variable]['type'] == 'number') {
                    $value = (float)$value;
                } elseif (in_array($pluginvars[$variable]['type'], array('forums', 'groups', 'selects'))) {
                    $value = serialize($value);
                }
                $value = (string)$value;



                C::t('common_pluginvar')->update_by_variable($pluginid, $variable, array('value' => $value));
            }
        }
    }

    if ($_FILES['wx_logo']['tmp_name']) {
        $upload = new discuz_upload();
        if (!$upload->init($_FILES['wx_logo'], 'common', random(3, 1), random(8)) || !$upload->save()) {
//                        var_dump($upload);exit;
            cpmsg($upload->errormessage(), '', 'error');
        }
        $parsev = parse_url($_G['setting']['attachurl']);
        $img = ($parsev['host'] ? '' : $_G['siteurl']) . $_G['setting']['attachurl'] . 'common/' . $upload->attach['attachment'];
        C::t('common_pluginvar')->update_by_variable($pluginid, 'wx_logo', array('value' => $img));
    }


    if($_GET['subscribe']){
        $settings = array('subscribe_reply' => $_GET['subscribe']);
        C::t('common_setting')->update_batch($settings);
    }

    updatecache(array('plugin', 'setting', 'styles'));
    cleartemplatecache();
    cpmsg('plugins_setting_succeed', 'action=plugins&operation=config&do=' . $pluginid . '&anchor=' . $anchor, 'succeed');

}
